package com.forum.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.forum.annotation.IgnoreAuth;
import com.forum.entity.ExamtabEntity;
import com.forum.entity.view.ExamtabView;
import com.forum.service.ExamtabService;
import com.forum.utils.MPUtil;
import com.forum.utils.PageUtils;
import com.forum.utils.R;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 试题表 前端控制器
 * </p>
 *
 * @author 张继成
 * @since 2023-12-31
 */
@RestController
@RequestMapping("/examquestion")
@Api(tags = "试题表控制")
public class ExamtabController {
    @Autowired
    private ExamtabService examquestionService;


    /**
     * 后端列表
     */
    @RequestMapping("/page")
    @SuppressWarnings("unchecked")
    public R page(@RequestParam Map<String, Object> params, ExamtabEntity examquestion,
                  HttpServletRequest request){
        EntityWrapper<ExamtabEntity> ew = new EntityWrapper<ExamtabEntity>();

        PageUtils page = examquestionService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, examquestion), params), params));

        return R.ok().put("data", page);
    }

    /**
     * 前端列表
     */
    @IgnoreAuth
    @RequestMapping("/list")
    @SuppressWarnings("unchecked")
    public R list(@RequestParam Map<String, Object> params, ExamtabEntity examquestion,
                  HttpServletRequest request){
        EntityWrapper<ExamtabEntity> ew = new EntityWrapper<ExamtabEntity>();

        PageUtils page = examquestionService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, examquestion), params), params));
        return R.ok().put("data", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/lists")
    @SuppressWarnings("unchecked")
    public R list(ExamtabEntity examquestion){
        EntityWrapper<ExamtabEntity> ew = new EntityWrapper<ExamtabEntity>();
        ew.allEq(MPUtil.allEQMapPre( examquestion, "examquestion"));
        return R.ok().put("data", examquestionService.selectListView(ew));
    }

    /**
     * 查询
     */
    @RequestMapping("/query")
    @SuppressWarnings("unchecked")
    public R query(ExamtabEntity examquestion){
        EntityWrapper< ExamtabEntity> ew = new EntityWrapper< ExamtabEntity>();
        ew.allEq(MPUtil.allEQMapPre( examquestion, "examquestion"));
        ExamtabView examquestionView =  examquestionService.selectView(ew);
        return R.ok("查询试题表成功").put("data", examquestionView);
    }

    /**
     * 后端详情
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
        ExamtabEntity examquestion = examquestionService.selectById(id);
        return R.ok().put("data", examquestion);
    }

    /**
     * 前端详情
     */
    @IgnoreAuth
    @RequestMapping("/detail/{id}")
    public R detail(@PathVariable("id") Long id){
        ExamtabEntity examquestion = examquestionService.selectById(id);
        return R.ok().put("data", examquestion);
    }




    /**
     * 后端保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody ExamtabEntity examquestion, HttpServletRequest request){
        examquestion.setId(new Date().getTime()+new Double(Math.floor(Math.random()*1000)).longValue());
        //ValidatorUtils.validateEntity(examquestion);
        examquestionService.insert(examquestion);
        return R.ok();
    }

    /**
     * 前端保存
     */
    @RequestMapping("/add")
    public R add(@RequestBody ExamtabEntity examquestion, HttpServletRequest request){
        examquestion.setId(new Date().getTime()+new Double(Math.floor(Math.random()*1000)).longValue());
        //ValidatorUtils.validateEntity(examquestion);
        examquestionService.insert(examquestion);
        return R.ok();
    }



    /**
     * 修改
     */
    @RequestMapping("/update")
    @Transactional
    public R update(@RequestBody ExamtabEntity examquestion, HttpServletRequest request){
        //ValidatorUtils.validateEntity(examquestion);
        examquestionService.updateById(examquestion);//全部更新
        return R.ok();
    }




    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
        examquestionService.deleteBatchIds(Arrays.asList(ids));
        return R.ok();
    }
}
